/**
 * access配置模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.apps.sw.bcAccessTable.bcAccessTableManager', ['water.entity.bcDevice','water.entity.bcAccessTable']);
		
	module.controller('bcAccessTableManagerController',function($scope,$bcSendPlatformService,$bcAccessTableService,$state){
		
		
		
		$scope.isShowList = true;
		
		$scope.isShowAdd = false;
		
		$scope.isShowEdit = false;
		
		$scope.addModel;
		
		$scope.accessParamList;


        $scope.CreateModel = {};
        $scope.EditModel = {};
        
        
        /**
         *初始值固定
         */
        $scope.CreateModel.DAYTABLE = "DayT_Report";
        $scope.CreateModel.HOURTABLE = "HourT_Report";
        $scope.CreateModel.MINTABLE = "MinT_Report";
        $scope.CreateModel.RTTABLE = "RealT_Report";
        $scope.CreateModel.DEVICETABLE = "DeviceOperInfoT_Report";
        $scope.CreateModel.DATATIME = "DATATIME";
        $scope.CreateModel.MEAS = "MEAS";
        $scope.CreateModel.MEASSIGIN = "MEAS_SIGIN";
        
        
        $scope.onCreateModel = function(){
			$scope.addModel = {};
			$scope.showAdd();
		}
		
        /**
         * 获取传输列表的数据,使传输列表的ID和名称与access库表的名称关联
         */
		$scope.getDeviceData = function () {
			$bcSendPlatformService.getModelList().then(function (data) {
				var deviceData = [];
				
                for (var i = 0; i < data.length; i++) {
                	deviceData.push({
                        k: data[i].PLATFORM_ID,
                        v: data[i].NAME
                    });
                }
                $scope.deviceData = deviceData;
                $scope.deviceDataModel.setData(deviceData);
                $scope.editDeviceDataModel.setData(deviceData);
            });
        };
        
        /**
         * 新增
         */
        $scope.onCreateModelSubmit = function (data){
   
        	for(var i=0;i<$scope.deviceData.length;i++){
        		if(data.ID==$scope.deviceData[i].k){
        			data.PLATFORMNAME = $scope.deviceData[i].v;
        		}
        	}
        	
        	$bcAccessTableService.create(data).then(function(){
        		$scope.getList();
				$scope.showList();
	        	alert("新增成功");
        	}).catch(function(error){
				alert(error);
			});
        };
        
        /**
         * 编辑
         */
        $scope.onEditModelSubmit = function (data){
            for(var i=0;i<$scope.deviceData.length;i++){
                if(data.ID==$scope.deviceData[i].k){
                    data.PLATFORMNAME = $scope.deviceData[i].v;
                }
            }

        	$bcAccessTableService.update(data).then(function(){
        		$scope.getList();
				$scope.showList();
	        	alert("新增成功");
        	}).catch(function(error){
				alert(error);
			});
        };
        
        /**
         * 删除
         */
        $scope.onDeleteModelSubmit = function(data){
        	
        	$bcAccessTableService.deleteModel(data.ID).then(function(){
        		$scope.getList();
				$scope.showList();
        	})
        };
        
        
        $scope.getList = function(){
        	
        	$bcAccessTableService.getModels().then(function(data){
        		$scope.accessParamList = data;
        	});
        };
        
        
        $scope.onEditModel = function(model){
        	
        	$scope.EditModel = model;
        	$scope.showEdit();
        };
        
        
        $scope.onCancelClick = function(){
			$scope.showList();
			
		};
        
        

        
        /**
		 * 只显示列表
		 */
		$scope.showList = function(){
			$scope.isShowList = true;
			$scope.isShowAdd = false;
			$scope.isShowEdit = false;
		};
		
		/**
		 * 只显示新建页
		 */
		$scope.showAdd = function(){
			$scope.isShowList = false;
			$scope.isShowAdd = true;
			$scope.isShowEdit = false;
		};
		
		/**
		 * 只显示编辑页
		 */
		$scope.showEdit = function(){
			$scope.isShowList = false;
			$scope.isShowAdd = false;
			$scope.isShowEdit = true;
		};
        
            
        
        $scope.getDeviceData();
        $scope.getList();
		
	});
	
}(window, angular));


